//
//  WmcQRCodeVerifyView.swift
//  YPWatermarkCamera
//
//  Created by Json on 2023/7/26.
//

import UIKit

class WmcQRCodeVerifyView: WmcBaseView {

    /// 二维码背景
    private lazy var qrcodeBgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.wmc_white_FFFFFF
        return view
    }()
    /// 二维码图片
    private(set) lazy var qrcodeView: UIImageView = {
        let imgView = UIImageView()
        imgView.image = "icon_qr_verify_52".wmc_image
        // 影响二维码识别
        imgView.contentMode = .scaleAspectFit
        return imgView
    }()
    /// 文字
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = 13.wmc_scaleFont(weight: .medium)
        label.textColor = UIColor.wmc_white_FFFFFF
        label.textAlignment = .center
        label.text = "扫码验证"
        return label
    }()
    
    var image: UIImage? {
        didSet {
            qrcodeView.image = image
        }
    }
    
    let padding: CGFloat = 2.0
    
    // MARK: UI
    override func makeUI() {
        super.makeUI()
        backgroundColor = UIColor.clear
        // 二维码背景
        addSubview(qrcodeBgView)
        qrcodeBgView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(qrcodeBgView.snp.width)
        }
        // 二维码图片
        qrcodeBgView.addSubview(qrcodeView)
        qrcodeView.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(UIEdgeInsets(top: padding, left: padding, bottom: padding, right: padding))
        }
        // 文字
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
        }
    }

}

extension WmcQRCodeVerifyView {
    
    
    /// 根据二维码图片的宽度,计算控件宽高
    /// - Parameter qrImageW: 二维码图片宽度
    func size(with qrImageW: CGFloat) -> CGSize {
        // 宽度最低不低于60(避免文字显示不下),最高不超过80
        var qr_width = max(qrImageW, 60.0.wmc_autoHor)
        qr_width = min(80.0.wmc_autoHor, qr_width)
        // 文字高度
        let textH = 20.0.wmc_autoHor
        // 二维码验证控件宽高
        // 宽度缩放比例
        let width = (qr_width + padding * 2.0)
        let height = width + textH
        return CGSize(width: width, height: height)
    }
}
